package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BDeviceQuantitiesEntity;
import com.hongxun.modules.api.service.BDeviceQuantitiesService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.APBaseService;
import com.hongxun.modules.bss.service.DDeviceBuyService;
import com.hongxun.modules.bss.service.DMaterialCostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.service.DDeviceOldService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 点位设备拆除技改大修
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-04 10:42:47
 */
@RestController
@RequestMapping("/bss/ddeviceold")
public class DDeviceOldController {
	@Autowired
	private DDeviceOldService dDeviceOldService;
	@Autowired
	private DMaterialCostService dMaterialCostService;
	@Autowired
	private APBaseService aPBaseService;
	@Autowired
	private BDeviceQuantitiesService bDeviceQuantitiesService;
	@Autowired
	private DDeviceBuyService dDeviceBuyService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";

		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  d_r_name like  '%" + query.get("qName").toString()+"%' ";
		}
		if(query.get("qLabel")!=null&&!query.get("qLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
		}


		wheres+=" and  d_r_datastate != '删除' ";
        query.put("filterSql",wheres+" ");
		List<DDeviceOldEntity> dDeviceOldList = dDeviceOldService.queryList(query);
		int total = dDeviceOldService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dDeviceOldList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DDeviceOldEntity dDeviceOld = dDeviceOldService.queryObject(guid);
		
		return R.ok().put("dDeviceOld", dDeviceOld);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DDeviceOldEntity dDeviceOld){
		dDeviceOldService.save(dDeviceOld);
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(dDeviceOld.getPGuid());
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  d_guid  = '" + dDeviceOld.getDDGuid() + "' ";
		wheres += " and  d_q_type =  " + aPBaseEntity.getPBType()  ;
		wheres += " and  d_q_work = 2 ";
		mapaPOther.put("filterSql", wheres + " ");
		List<BDeviceQuantitiesEntity> listBDeviceQuantitiesEntity=	bDeviceQuantitiesService.queryList(mapaPOther);
		for(BDeviceQuantitiesEntity bDeviceQuantitiesEntity :listBDeviceQuantitiesEntity)
		{
			DMaterialCostEntity dMaterialCostEntity = new DMaterialCostEntity();
			dMaterialCostEntity.setPPGuid(dDeviceOld.getPPGuid());
			dMaterialCostEntity.setPGuid(dDeviceOld.getPGuid());
			dMaterialCostEntity.setPMGuid(dDeviceOld.getGuid());
			dMaterialCostEntity.setMMGuid(dDeviceOld.getDDGuid());
			dMaterialCostEntity.setMCWork("2");
			dMaterialCostEntity.setMCQuantities(bDeviceQuantitiesEntity.getDQQuantities());
			dMaterialCostEntity.setMCExplain(bDeviceQuantitiesEntity.getDQExplain());
			dMaterialCostEntity.setMCUnit(bDeviceQuantitiesEntity.getDQUnit());
			dMaterialCostEntity.setMCMeterUnit(bDeviceQuantitiesEntity.getDQMeterUnit());
			dMaterialCostEntity.setMCCount(new BigDecimal(1) );
			dMaterialCostEntity.setMCPrice(bDeviceQuantitiesEntity.getDQPrice());
			dMaterialCostEntity.setMCPriceall(bDeviceQuantitiesEntity.getDQPrice());
			dMaterialCostEntity.setMCRatio(bDeviceQuantitiesEntity.getDQRatio());
			dMaterialCostEntity.setMCDatastate("新增");
			dMaterialCostEntity.setMMName(dDeviceOld.getDRName());
			dMaterialCostEntity.setMMType(0);
			dMaterialCostEntity.setPMSource(1);
			dMaterialCostService.save(dMaterialCostEntity);
		}
		return R.ok();

	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DDeviceOldEntity dDeviceOld){
		dDeviceOldService.update(dDeviceOld);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dDeviceOldService.deleteBatch(guids);

		dMaterialCostService.deleteBatchByMBguid(guids);
		List<DDeviceBuyEntity> dDeviceBuyEntityList =	dDeviceBuyService.queryListByDDguid(guids);
      if(dDeviceBuyEntityList.size()>0)
	  {
	  	  String [] d  = new String [dDeviceBuyEntityList.size()];
	  	  for(int i=0;i<dDeviceBuyEntityList.size();i++)
		  {
                 d[i]=dDeviceBuyEntityList.get(i).getGuid();
		  }
		  dDeviceBuyService.deleteBatch(d);
	  	  dMaterialCostService.deleteBatchByMBguid(d);
       }
		return R.ok();
	}


	/**
	 * 	拆除表
	 * @param params
	 * @return
	 */
	@RequestMapping("/removeList")
	public R removeList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		String wheres2 =" 1=1 ";

		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
			wheres2+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  d_r_name like  '%" + query.get("qName").toString()+"%' ";
			wheres2+=" and  m_b_name like  '%" + query.get("qName").toString()+"%' ";
		}
		if(query.get("qLabel")!=null&&!query.get("qLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
			wheres2+=" and  d_label like '%" + query.get("qLabel").toString()+"%' ";
		}


		wheres+=" and d_r_datastate != '删除' and d_r_work = '拆除' ";
		wheres2+=" and m_b_datastate != '删除' and m_b_work = '拆除' ";
		query.put("filterSql",wheres+" ");
		query.put("filterSql2",wheres2+" ");
		List<RemoveEntity> dDeviceOldList = dDeviceOldService.removeList(query);
		int total = dDeviceOldService.queryTotalForRemove(query);

		PageUtils pageUtil = new PageUtils(dDeviceOldList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}



}
